﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Siva.CBC.Barnacle.ashx
{
    /// <summary>
    /// cunSearchHandler 的摘要说明
    /// </summary>
    public class cunSearchHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            //1.接收参数
            string province = context.Request.Form["province"];
            string city = context.Request.Form["city"];
            string district = context.Request.Form["district"];
            string town = context.Request.Form["town"];
            string village = context.Request.Form["village"];

            cunReturn returnData = new cunReturn();

            //2.验证参数
            if (string.IsNullOrEmpty(village))
            {
                returnData.Message = "非法参数";
                context.Response.Write(JsonSerialize.Serialize(returnData));
                return;
            }

            //3.拼接查询字符串
            string sqlstr = "select province,city,area,townName,villageName,Name,Number,Memo from jsnc where 1=1 ";
            if (!string.IsNullOrEmpty(province))
            {
                sqlstr += string.Format("and province like '%{0}%'", province);
            }
            if (!string.IsNullOrEmpty(city))
            {
                sqlstr += string.Format("and city like '%{0}%'", city);
            }
            if (!string.IsNullOrEmpty(district))
            {
                sqlstr += string.Format("and area like '%{0}%'", district);
            }
            if (!string.IsNullOrEmpty(town))
            {
                sqlstr += string.Format("and townName like '%{0}%'", town);
            }
            if (!string.IsNullOrEmpty(village))
            {
                sqlstr += string.Format("and villageName like '%{0}%'", village);
            }
            sqlstr += " order by province,city,area,townName";
            var dt = SqlHelper.ExecuteDataTable(sqlstr);

            if (dt.Rows.Count <= 0)
            {
                returnData.Message = "未查询到记录";
                context.Response.Write(JsonSerialize.Serialize(returnData));
                return;
            }

            List<cunModel> list = new List<cunModel>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cunModel model = new cunModel();
                model.province = dt.Rows[i][0].ToString();
                model.city = dt.Rows[i][1].ToString();
                model.district = dt.Rows[i][2].ToString();
                model.town = dt.Rows[i][3].ToString();
                model.village = dt.Rows[i][4].ToString();
                model.name = dt.Rows[i][5].ToString();
                model.tel = dt.Rows[i][6].ToString();
                model.Memo = dt.Rows[i][7].ToString();
                list.Add(model);
            }

            returnData.Success = true;
            returnData.Data = JsonSerialize.Serialize(list);

            context.Response.Write(JsonSerialize.Serialize(returnData));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}